﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SymmetricEncription.Controls.AesControls;
using System.Windows;
using Org.BouncyCastle.Crypto.Utilities;

namespace SymmetricEncription.Presenters.AesSteps
{
    public class KeyExpansionStep : ProcessStepBase
    {
        private byte[] key = new byte[0];
        public byte[] Key
        {
            get { return key; }
        }

        private int nk = 0;
        public int Nk
        {
            get { return nk; }
            set
            {
                if (nk != value)
                {
                    nk = value;
                    RaisePropertyChanged("Nk");
                }
            }
        }
        
        private int nb = 4;
        public int Nb
        {
            get { return nb; }
        }

        private int nr = 0;
        public int Nr
        {
            get { return nr; }
            set
            {
                if (nr != value)
                {
                    nr = value;
                    RaisePropertyChanged("Nr");
                }
            }
        }

        private uint[,] w1 = new uint[0, 0];
        public uint[,] W1
        {
            get { return w1; }
            set {
                if (w1 != value)
                {
                    w1 = value;
                    RaisePropertyChanged("W1");
                    RaisePropertyChanged("PlainW1");
                }
            }
        }

        private uint[,] w2 = new uint[0, 0];
        public uint[,] W2
        {
            get { return w2; }
            set
            {
                if (w2 != value)
                {
                    w2 = value;
                    RaisePropertyChanged("W2");
                    RaisePropertyChanged("PlainW2");
                }
            }
        }

        public KeyExpansionStep(ProcessStepBase previousStep, byte[] key) : base(previousStep)
        {
            this.key = key;
            this.Title = "Расширение ключа";
        }

        private KeyExpansion stepUIControl = new KeyExpansion();
        public override UIElement StepUIControl
        {
            get { return this.stepUIControl; }
        }

        public byte[] PlainW1
        {
            get
            {
                try
                {
                    byte[] result = new byte[this.w1.GetLength(0) * this.w1.GetLength(1) * 4];
                    for (int i = 0; i < this.w1.GetLength(0); i++)
                    {
                        for (int j = 0; j < this.w1.GetLength(1); j++)
                        {
                            Pack.UInt32_To_LE(this.w1[i, j], result, (i * this.w1.GetLength(1) * 4) + j * 4);
                        }
                    }
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }

        public byte[] PlainW2
        {
            get
            {
                try
                {
                    byte[] result = new byte[this.w2.GetLength(0) * this.w2.GetLength(1) * 4];
                    for (int i = 0; i < this.w2.GetLength(0); i++)
                    {
                        for (int j = 0; j < this.w2.GetLength(1); j++)
                        {
                            Pack.UInt32_To_LE(this.w2[i, j], result, (i * this.w2.GetLength(1) * 4) + j * 4);
                        }
                    }
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }

        private byte[] s;
        public byte[] S
        {
            get { return s; }
            set
            {
                if (s != value)
                {
                    s = value;
                    RaisePropertyChanged("S");
                }
            }
        }

        private byte[] rCon;
        public byte[] RCon
        {
            get { return rCon; }
            set
            {
                if (rCon != value)
                {
                    rCon = value;
                    RaisePropertyChanged("RCon");
                }
            }
        }
    }
}
